package edu.sdbi.judge.controller;

import com.alibaba.fastjson.JSONObject;
import edu.sdbi.judge.domain.vo.JsonResult;
import edu.sdbi.judge.util.ErrorUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/statistics")
public class StaticData12Controller {
    public static final Map<String, Object> mockData12 = new HashMap<>();

    static {
        // 初始化模拟数据
        // mockData.put("newsId", "1");
        mockData12.put("1", 65);
//        mockData.put("visitCount", 500);
//        mockData.put("CommentCount", 3.5);
    }

    /**
     * 模拟新闻访问统计接口
     *
     * @param newsID 新闻ID
     * @return 统计结果
     */
    @PostMapping("/comment")
    public String getNewsComment(@RequestParam("newsID") String newsID) {
        try {
            // 验证新闻ID
            if (!mockData12.containsKey(newsID)) {
                return buildErrorResult(400, "无效的新闻ID");
            }

            // 构建成功响应
            JsonResult result = new JsonResult();
            result.setCode(200);
            result.setMsg("成功");
            result.setData(mockData12.get(newsID));

            return JSONObject.toJSONString(result);
        } catch (Exception e) {
            return buildErrorResult(500, ErrorUtil.stackTraceToString(e));
        }
    }

    /**
     * 构建错误响应
     *
     * @param code    错误码
     * @param message 错误信息
     * @return 错误响应JSON字符串
     */
    private String buildErrorResult(int code, String message) {
        JsonResult result = new JsonResult();
        result.setCode(code);
        result.setMsg(message);
        return JSONObject.toJSONString(result);
    }
}
